import PeripheralSystemDataMgr from "./PeripheralSystemDataMgr";
import ListItem from "./ListItem";
import EngineUtil from "./EngineUtil";
import PageMgr from "./PageMgr";
import UiManage from "./UiManage";
import FundWithDrawProgressItem from "./FundWithDrawProgressItem";

const {ccclass, property, menu} = cc._decorator;

@ccclass
@menu("UI/prefabs/FundWithDrawProgressItemCtrl")
export default class FundWithDrawProgressItemCtrl extends ListItem {
    @property(cc.Node)
    ui = null;
    city_info = null;

    onLoad() {
        this.onUILoad();
        this.addButtonListen();
    }

    onUILoad() {
        this.ui = this.node.addComponent(FundWithDrawProgressItem);
    }

    addButtonListen() {
        UiManage.addButtonListen(this.ui.bgNode, this.showTips, this);
    }

    showTips() {
        PageMgr.showPage("TravelToastPage", {
            type: 3,
            cityId: null
        });
    }

    setData(e) {
        this.initData(e);
    }

    initData(e) {
        if (e) {
            this.city_info = e;
            this.initUI();
        }
    }

    initUI() {
        var e = PeripheralSystemDataMgr.getCurrentLocation(),
            t = "<b><color=#785740>到达第</color><color=#FF3A17>cityId</color><color=#785740>个城市</color><color=#FF3A17>cityName</color></b>",
            a = this.city_info.city_id,
            o = PeripheralSystemDataMgr.getCityNameByCityId(parseInt(a) - 1),
            i = EngineUtil.getCashBalance(this.city_info.REWARD_MONEY);
            
        t = t.replace(/cityId/g, a).replace(/cityName/g, o);
        this.ui.desc.getComponent(cc.RichText).string = t;
        this.ui.cashLabel.getComponent(cc.Label).string = i + "元";

        var r = e / parseInt(a),
            l = EngineUtil.getProgressWidth(r, 25, 306).width;
        this.ui.pro.width = l;
        this.ui.proLabel.getComponent(cc.Label).string = e + "/" + a;

        this.adjustWidth();
    }

    adjustWidth() {
        var e = this.ui.desc.width,
            t = 1;
        if (e >= 300) {
            t = 300 / e;
        }
        this.ui.desc.scale = t;
    }
}

FundWithDrawProgressItemCtrl.prefabUrl = "assets/resources/prefabs/FundWithDrawProgressItem";
FundWithDrawProgressItemCtrl.className = "FundWithDrawProgressItemCtrl";
